<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/5/11
 * Time: 上午9:41
 */

namespace App\Busi\Modules\Resource;


use App\Busi\Modules\BaseBusi;
use App\Busi\Traits\DistTrait;
use App\Http\Requests\CityBusiListRo;
use App\Models\Company;
use App\Models\Dist;

class CityListBusi extends BaseBusi
{
    use DistTrait;
    public function handle(CityBusiListRo $ro){
        //先获取公司列表里的所有城市id
        $where = [
//            ['flag','=',3],
            ['city_id','>',0]
        ];
        $fields = ['city_id'];
        $cityIds = Company::where($where)->select($fields)->groupBy('city_id')->get();
        $cityIds = $cityIds->toArray();
        //获取城市id对应的城市名字
        $fields2 = ['id','dist_name','parent_id'];
        $dist = Dist::wherein('id',$cityIds)->select($fields2)->get();
        $cityList = [];
        foreach ($dist as $item){
            $city_item = [];
            if($item['dist_name'] == "市辖区"){
                $dist_item = $this->getDistByPid($item['parent_id']);
                $city_item['id'] = $dist_item['id'];
                $city_item['dist_name'] = $dist_item['dist_name'];
                array_push($cityList,$city_item);
            }else{
                $city_item['id'] = $item['id'];
                $city_item['dist_name'] = $item['dist_name'];
                array_push($cityList,$city_item);
            }

        }
        return $cityList;
    }

}